/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package util;

import java.util.Iterator;

/**
 *
 * @author Administrator
 */
public class StringExtensions {

    public static <T> String join(Iterable<T> s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        Iterator iter = s.iterator();
        while (iter.hasNext()) {
            buffer.append(iter.next());
            if (iter.hasNext()) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }

    public static String join(char[] s, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        int len = s.length;
        for (int i = 0; i < len; i++) {
            buffer.append(s[i]);
            if (i < len - 1) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }

    public static String join(char[] s, char delimiter) {
        return join(s, String.valueOf(delimiter));
    }

    public static String join(String[] list, String delimiter) {
        StringBuffer buffer = new StringBuffer();
        for (int i = 0; i < list.length; i++) {
            buffer.append(list[i]);
            if (i < list.length - 1) {
                buffer.append(delimiter);
            }
        }
        return buffer.toString();
    }
}
